package com.example.demo.common;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * 密码工具类
 * 加盐加密&&加盐解密
 */
public class PasswordTools {
    //加密
    //password是明文密码  要返回一个加盐加密的密码
    public static String encrypt(String password){
        //1.产生盐值
        String salt = UUID.randomUUID().toString().replace("-","");
        //2.使用盐值+明文密码得到
        String finalPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes(StandardCharsets.UTF_8));
        //3.将盐值和加密的密码共同返回（合并盐值和加密密码）
        String dbPassword = salt +"$"+finalPassword;
        return dbPassword;
    }

    /**
     * 加盐加密
     * @param password  明文密码
     * @param salt  盐值
     * @return  加盐加密的密码
     */
    public static String encrypt(String password,String salt){
        //1.使用盐值+明文密码得到
        String finalPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes(StandardCharsets.UTF_8));
        //2.将盐值和加密的密码共同返回（合并盐值和加密密码）
        String dbPassword = salt +"$"+finalPassword;
        return dbPassword;
    }
    /**
     * 验证加盐加密密码
     * @param password  明文密码（不一定对 ，当前用户输入的密码
     * @param dbPassword  数据库密码（包含：salt+$+加盐加密密码
     * @return true = 密码正确  false = 密码失败
     */
    public static boolean decrypt(String password,String dbPassword){
        boolean result = false;
        if (StringUtils.hasLength(password)&&StringUtils.hasLength(dbPassword)&&
        dbPassword.length()==65&&dbPassword.contains("$")){
            //参数正确
            //0.得到盐值
            String[] passwordArr = dbPassword.split("\\$");
            //盐值
            String salt = passwordArr[0];
            //得到正确的加盐加密密码
            String finalPassword = passwordArr[1];
            //1.生成验证密码的加盐加密密码
            String checkPassword = encrypt(password,salt);

            if (dbPassword.equals(checkPassword)){
                result = true;
            }
        }
        return result;
    }


    public static void main(String[] args) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = "123456";
//        String dbPassword = PasswordTools.encrypt(password);
//        String dbPassword1 = PasswordTools.encrypt(password);
//        String dbPassword2 = PasswordTools.encrypt(password);
        String dbPassword = passwordEncoder.encode(password);
        String dbPassword1 = passwordEncoder.encode(password);
        String dbPassword2 = passwordEncoder.encode(password);


        System.out.println(dbPassword);
        System.out.println(dbPassword1);
        System.out.println(dbPassword2);

        String cPwd = "12345";
        boolean result = passwordEncoder.matches(cPwd,dbPassword2);

        System.out.println("错误的对比结果："+result);
        String cPwd1 = "123456";
        boolean result1 = passwordEncoder.matches(cPwd1,dbPassword);

        System.out.println("正确的对比结果："+result1);
    }
}


